package com.citydo.webrtcspringboot.transfer.udp;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.*;

@Slf4j
@Component
public class UdpClient {
    @Value("127.0.0.1")
    private String host;
    @Value("11301")
//    @Value("9090")
    private String port;
    private DatagramSocket socket;
    byte[] buffer = new byte[1024];
    DatagramPacket packet ;
    public UdpClient(){
    }
    @PostConstruct
    public void Init() throws SocketException, UnknownHostException {
        socket = new DatagramSocket();
        log.info("port:"+port);
        packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(host),Integer.parseInt(port));
    }
    public void SendUdp(String message) throws IOException {
//        log.info("发送消息:"+message);
        packet.setData(message.getBytes());
        socket.send(packet);
    }
}
